import Taro, { Chain } from "@tarojs/taro";

const apiConfig = {
  baseUrl: "https://rickandmortyapi.com/api",
};
const reqInterceptor = (chain: Chain) => {
  const requestParams = chain.requestParams;
  // let token = Taro.getStorageSync('TOKEN')//拿到本地缓存中存的token
  requestParams.url = `${apiConfig.baseUrl}${requestParams.url}`;
  requestParams.header = {
    ...requestParams.header,
    // Authorization: 'Bearer ' + token //将token添加到头部
  };
  return chain.proceed(requestParams).then((res) => {
    return res;
  });
};
Taro.addInterceptor(reqInterceptor);
Taro.addInterceptor(Taro.interceptors.timeoutInterceptor);
type reqParams = Parameters<typeof Taro.request>[0];

const httpService = async <T,>(params: reqParams) => {
  const { method, url, header = {}, ...rest } = params;
  const res = await Taro.request<T>({
    method,
    url,
    header: {
      "content-type": "application/json",
      ...header,
    },
    ...rest,
  });
  return res.data;
};

interface characterParams {}
export const getCharacter = async (params: characterParams) => {
  const res = await httpService<any>({ method: "GET", url: "/character" });
  return res.results;
};
